<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Código-Fonte de selection_by_roulette.h</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javaScript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body onload='searchBox.OnSelectItem(0);'>
<!-- Gerado por Doxygen 1.6.3 -->
<script type="text/javascript"><!--
var searchBox = new SearchBox("searchBox", "search",false,'Busca');
--></script>
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;Principal</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Arquivos</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <img id="MSearchSelect" src="search/search.png"
             onmouseover="return searchBox.OnSearchSelectShow()"
             onmouseout="return searchBox.OnSearchSelectHide()"
             alt=""/>
        <input type="text" id="MSearchField" value="Busca" accesskey="S"
             onfocus="searchBox.OnSearchFieldFocus(true)" 
             onblur="searchBox.OnSearchFieldFocus(false)" 
             onkeyup="searchBox.OnSearchFieldChange(event)"/>
        <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
        </div>
      </li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>Lista&nbsp;de&nbsp;Arquivos</span></a></li>
      <li><a href="globals.html"><span>Arquivos&nbsp;Membros</span></a></li>
    </ul>
  </div>
<h1>selection_by_roulette.h</h1><a href="selection__by__roulette_8h.html">Vá para a documentação deste arquivo.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef SELECTION_BY_ROULETTE_H</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define SELECTION_BY_ROULETTE_H </span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;utility&gt;</span><span class="comment">//std::pair</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00014"></a>00014 
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;<a class="code" href="genetic__operator_8h.html">genetic_operator.h</a>&quot;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &quot;<a class="code" href="definitions_8h.html">definitions.h</a>&quot;</span>
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00025"></a>00025 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00026"></a>00026 <span class="keyword">template</span> &lt;<span class="keyword">typename</span> _ty = GAtype,<span class="keyword">typename</span> _realTy= GAreal_type&gt; <span class="keyword">class </span><a class="code" href="classselection__by__roulette.html">selection_by_roulette</a>; 
<a name="l00027"></a>00027 
<a name="l00028"></a>00028 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00035"></a>00035 <span class="comment">/* ----------------------------------------*/</span>
<a name="l00036"></a>00036 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00037"></a><a class="code" href="classselection__by__roulette.html">00037</a> <span class="keyword">class </span><a class="code" href="classselection__by__roulette.html">selection_by_roulette</a> : <span class="keyword">public</span> <a class="code" href="classgenetic__operator.html">genetic_operator</a>&lt;_ty,_realTy&gt;
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039   <span class="keyword">public</span>:
<a name="l00040"></a>00040 
<a name="l00041"></a>00041     <span class="comment">/* --------------------------------------------------------------- *</span>
<a name="l00042"></a>00042 <span class="comment">     * --- Algumas definições básicas da classe                        *</span>
<a name="l00043"></a>00043 <span class="comment">     * --------------------------------------------------------------- */</span>
<a name="l00044"></a>00044 
<a name="l00045"></a>00045     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00049"></a>00049     <span class="comment">/* ----------------------------------------*/</span>
<a name="l00050"></a><a class="code" href="classselection__by__roulette.html#a4c224a095113f38dce7d98f10d8c0833">00050</a>    <span class="keyword">typedef</span> <span class="keyword">typename</span> std::vector&lt;std::pair&lt;_realTy,individual&lt;_ty,_realTy&gt;*&gt; &gt; <a class="code" href="classselection__by__roulette.html#a4c224a095113f38dce7d98f10d8c0833">_RouletteTy</a>;
<a name="l00051"></a>00051 
<a name="l00052"></a>00052    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00056"></a>00056    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00057"></a><a class="code" href="classselection__by__roulette.html#a557d2432eb36aba61aa31a9e4b0dcb54">00057</a>    <span class="keyword">typedef</span> <span class="keyword">typename</span> std::vector&lt;std::pair&lt;_realTy,individual&lt;_ty,_realTy&gt;*&gt; &gt;::iterator <a class="code" href="classselection__by__roulette.html#a557d2432eb36aba61aa31a9e4b0dcb54">it_</a>;
<a name="l00058"></a>00058 
<a name="l00059"></a>00059    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00063"></a>00063    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00064"></a><a class="code" href="classselection__by__roulette.html#ac64df0d86cf8396ce91df17d9ce27f0e">00064</a>    <span class="keyword">typedef</span> <span class="keyword">typename</span> std::vector&lt;std::pair&lt;_realTy,individual&lt;_ty,_realTy&gt;*&gt; &gt;::const_iterator <a class="code" href="classselection__by__roulette.html#ac64df0d86cf8396ce91df17d9ce27f0e">const_it_</a>;
<a name="l00065"></a>00065 
<a name="l00066"></a>00066    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00072"></a>00072    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00073"></a><a class="code" href="classselection__by__roulette.html#a043cefe0479622491fea593c7f28ddfa">00073</a>    <a class="code" href="classselection__by__roulette.html#a043cefe0479622491fea593c7f28ddfa">selection_by_roulette</a>(<a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;</a>* pt_to_apply_operator = NULL):<a class="code" href="classgenetic__operator.html">genetic_operator</a>&lt;_ty,_realTy&gt;::<a class="code" href="classgenetic__operator.html">genetic_operator</a>(pt_to_apply_operator){}
<a name="l00074"></a>00074 
<a name="l00075"></a>00075    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00081"></a>00081    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00082"></a><a class="code" href="classselection__by__roulette.html#a137f1a757ab4d71a0ca0bdba7f9f35d0">00082</a>    <span class="keyword">virtual</span> std::string <a class="code" href="classselection__by__roulette.html#a137f1a757ab4d71a0ca0bdba7f9f35d0">GetName</a> (<span class="keywordtype">void</span>) {<span class="keywordflow">return</span> <a class="code" href="classselection__by__roulette.html#a137f1a757ab4d71a0ca0bdba7f9f35d0">genetic_operator&lt;_ty,_realTy&gt;::GetName</a>() + <span class="stringliteral">&quot;-&gt;&quot;</span> + <span class="stringliteral">&quot;selection_by_roulette&quot;</span>;}
<a name="l00083"></a>00083 
<a name="l00084"></a>00084    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00092"></a>00092    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00093"></a><a class="code" href="classselection__by__roulette.html#abb536fd7b63a452a2ebd8e7572bfc4d8">00093</a>    <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keywordtype">char</span>&amp; <a class="code" href="classselection__by__roulette.html#abb536fd7b63a452a2ebd8e7572bfc4d8">WalkOnIndividualHook</a> (<a class="code" href="classcoordinate.html">coordinate&lt;_ty,_realTy&gt;</a>&amp; coo) {<span class="keywordflow">return</span> def::genetic_operator::go_up;}
<a name="l00094"></a>00094 
<a name="l00095"></a>00095    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00103"></a>00103    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00104"></a>00104    <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="keywordtype">char</span>&amp; <a class="code" href="classselection__by__roulette.html#a98be3d54afb87450f190615f7da330e9">WalkOnPopulationHook</a> (<a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;</a>&amp; <span class="keywordtype">id</span>);
<a name="l00105"></a>00105 
<a name="l00106"></a>00106    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00112"></a>00112    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00113"></a><a class="code" href="classselection__by__roulette.html#a8d04a76229aa2d0c68c1a945e77d8e19">00113</a>    <a class="code" href="classselection__by__roulette.html#ac64df0d86cf8396ce91df17d9ce27f0e">const_it_</a> <a class="code" href="classselection__by__roulette.html#a8d04a76229aa2d0c68c1a945e77d8e19">begin</a> (<span class="keywordtype">void</span>)<span class="keyword"> const </span>{<span class="keywordflow">return</span> _myRoulette.begin();}
<a name="l00114"></a>00114 
<a name="l00115"></a>00115    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00121"></a>00121    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00122"></a><a class="code" href="classselection__by__roulette.html#a2abe33c34280ba81f0f0dab12c8e875b">00122</a>    <a class="code" href="classselection__by__roulette.html#ac64df0d86cf8396ce91df17d9ce27f0e">const_it_</a> <a class="code" href="classselection__by__roulette.html#a2abe33c34280ba81f0f0dab12c8e875b">end</a> (<span class="keywordtype">void</span>)<span class="keyword"> const </span>{<span class="keywordflow">return</span> _myRoulette.end();}
<a name="l00123"></a>00123 
<a name="l00124"></a>00124   <span class="keyword">protected</span>:
<a name="l00125"></a>00125 
<a name="l00126"></a>00126    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00130"></a>00130    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00131"></a>00131    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classselection__by__roulette.html#ac3db5bbe67ee2aa4e1942fd983d3cc80">GenerateRoulette</a> (<span class="keywordtype">void</span>);
<a name="l00132"></a>00132 
<a name="l00133"></a>00133    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00137"></a>00137    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00138"></a>00138    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classselection__by__roulette.html#ae19e7e752f54ab03983be567c8d10392">RotateRoulette</a> (<span class="keywordtype">void</span>);
<a name="l00139"></a>00139    
<a name="l00140"></a>00140   <span class="keyword">private</span>:
<a name="l00141"></a>00141 
<a name="l00142"></a>00142    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00146"></a>00146    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00147"></a>00147    <a class="code" href="classselection__by__roulette.html#a4c224a095113f38dce7d98f10d8c0833">_RouletteTy</a> _myRoulette;
<a name="l00148"></a>00148 
<a name="l00149"></a>00149    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00153"></a>00153    <span class="comment">/* ----------------------------------------*/</span>
<a name="l00154"></a>00154    <span class="keyword">typename</span> <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;::_pop</a> new_pop;<span class="comment">//nova população após a seleção</span>
<a name="l00155"></a>00155 
<a name="l00156"></a>00156 };
<a name="l00157"></a>00157 
<a name="l00158"></a>00158 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00159"></a><a class="code" href="classselection__by__roulette.html#a98be3d54afb87450f190615f7da330e9">00159</a> <span class="keyword">const</span> <span class="keywordtype">char</span>&amp; <a class="code" href="classselection__by__roulette.html#a98be3d54afb87450f190615f7da330e9">selection_by_roulette&lt;_ty,_realTy&gt;::WalkOnPopulationHook</a> (<a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;</a>&amp; <span class="keywordtype">id</span>)
<a name="l00160"></a>00160 {
<a name="l00161"></a>00161   <a class="code" href="classselection__by__roulette.html#ac3db5bbe67ee2aa4e1942fd983d3cc80">GenerateRoulette</a>();
<a name="l00162"></a>00162   <a class="code" href="classselection__by__roulette.html#ae19e7e752f54ab03983be567c8d10392">RotateRoulette</a>();
<a name="l00163"></a>00163   <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;::_to_apply_operator</a>-&gt;SetNewIndividuals(new_pop);
<a name="l00164"></a>00164   new_pop.clear();
<a name="l00165"></a>00165   <span class="keywordflow">return</span> def::genetic_operator::go_up;
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167 
<a name="l00168"></a>00168 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00169"></a><a class="code" href="classselection__by__roulette.html#ac3db5bbe67ee2aa4e1942fd983d3cc80">00169</a> <span class="keywordtype">void</span> <a class="code" href="classselection__by__roulette.html#ac3db5bbe67ee2aa4e1942fd983d3cc80">selection_by_roulette&lt;_ty,_realTy&gt;::GenerateRoulette</a> (<span class="keywordtype">void</span>)
<a name="l00170"></a>00170 {
<a name="l00171"></a>00171   _realTy partial = 0;  
<a name="l00172"></a>00172   _realTy sum = <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;::_to_apply_operator</a>-&gt;GetSum();
<a name="l00173"></a>00173   <span class="keywordflow">for</span> (<span class="keyword">typename</span> <a class="code" href="classpopulation.html">population&lt;_ty,_realTy&gt;::const_it_</a> it = <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;::_to_apply_operator</a>-&gt;<a class="code" href="classselection__by__roulette.html#a8d04a76229aa2d0c68c1a945e77d8e19">begin</a>();
<a name="l00174"></a>00174        it != <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;</a>:: _to_apply_operator-&gt;end(); ++it)
<a name="l00175"></a>00175   {
<a name="l00176"></a>00176     partial += (*it)-&gt;GetValue()/sum;
<a name="l00177"></a>00177     _myRoulette.push_back(std::make_pair(partial,*it));
<a name="l00178"></a>00178   }
<a name="l00179"></a>00179 }
<a name="l00180"></a>00180 
<a name="l00181"></a>00181 <span class="keyword">template</span>&lt;<span class="keyword">typename</span> _ty,<span class="keyword">typename</span> _realTy&gt;
<a name="l00182"></a><a class="code" href="classselection__by__roulette.html#ae19e7e752f54ab03983be567c8d10392">00182</a> <span class="keywordtype">void</span> <a class="code" href="classselection__by__roulette.html#ae19e7e752f54ab03983be567c8d10392">selection_by_roulette&lt;_ty,_realTy&gt;::RotateRoulette</a> (<span class="keywordtype">void</span>)
<a name="l00183"></a>00183 {
<a name="l00184"></a>00184   <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i != <a class="code" href="classgenetic__operator.html">genetic_operator&lt;_ty,_realTy&gt;::_to_apply_operator</a>-&gt;GetNumerOfIndividuals(); ++i)
<a name="l00185"></a>00185   {
<a name="l00186"></a>00186      _realTy coin = <a class="code" href="classgenetic__operator.html#a406eeb5144dd74698fcd9a120f021aba">genetic_operator&lt;_ty,_realTy&gt;::GenerateRandom</a>(); <span class="comment">//gera um numero aleatório de 0 a 1</span>
<a name="l00187"></a>00187      <span class="keywordflow">for</span> ( <a class="code" href="classselection__by__roulette.html#ac64df0d86cf8396ce91df17d9ce27f0e">const_it_</a> it = _myRoulette.begin(); it != _myRoulette.end(); ++ it)
<a name="l00188"></a>00188      {
<a name="l00189"></a>00189        <span class="keywordflow">if</span> (it-&gt;first &gt;= coin)
<a name="l00190"></a>00190        {
<a name="l00191"></a>00191          it-&gt;second-&gt;SetID(i);
<a name="l00192"></a>00192          <a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;</a> *new_id = <span class="keyword">new</span> <a class="code" href="classindividual.html">individual&lt;_ty,_realTy&gt;</a>(*it-&gt;second);
<a name="l00193"></a>00193          new_pop.push_back(new_id);
<a name="l00194"></a>00194          <span class="keywordflow">break</span>;<span class="comment">//sai do ultimo laço(laço do iterator)</span>
<a name="l00195"></a>00195        }
<a name="l00196"></a>00196      }
<a name="l00197"></a>00197   }
<a name="l00198"></a>00198   <span class="comment">//esvazia a roleta para a próxima seleção</span>
<a name="l00199"></a>00199   _myRoulette.clear();
<a name="l00200"></a>00200 }
<a name="l00201"></a>00201 
<a name="l00202"></a>00202 <span class="preprocessor">#endif //SELECTION_BY_ROULETTE_H</span>
</pre></div></div>
<!--- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&nbsp;</span>Todos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&nbsp;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&nbsp;</span>Arquivos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&nbsp;</span>Funções</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&nbsp;</span>Variáveis</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&nbsp;</span>Definições de Tipos</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&nbsp;</span>Amigas</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<hr class="footer"/><address style="text-align: right;"><small>Gerado em Sat Oct 30 13:47:50 2010 por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address>
</body>
</html>
